package com.lrm.biz.impl;

import com.lrm.biz.ITypeBiz;
import com.lrm.dao.TypeJPA;
import com.lrm.exception.MyException;
import com.lrm.pojo.Type;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

/**
 * @Author: 王金海
 * @Description:
 * @Date: Created by Administrator on 2017/11/30.
 * @Modified By:
 */
@Service
@Transactional
public class TypeBizImpl implements ITypeBiz {

    @Autowired
    private TypeJPA typeJPA;
    @Override
    public Type saveType(Type type) {
        Type save = typeJPA.save(type);
        return save;
    }

    @Override
    public Type getType(Long id) {
        return typeJPA.findOne(id);
    }

    @Override
    public Page<Type> pagelistType(Pageable pageable) {
        Page<Type> all = typeJPA.findAll(pageable);
        return all;
    }

    @Override
    public List<Type> listType(Integer size) {
        Sort sort =new Sort(Sort.Direction.DESC,"blogs.size");
        Pageable pageable = new PageRequest(0,size,sort);
        return typeJPA.listType(pageable);
    }


    @Override
    public Type upadtaType(Long id, Type type) {
        Type typeJPAOne = typeJPA.getOne(id);
        if (typeJPAOne==null) {
            throw new MyException("不存在该类型",2);
        }
        BeanUtils.copyProperties(typeJPAOne,type);
        Type save = typeJPA.save(typeJPAOne);
        return save;
    }

    @Override
    public Boolean delType(Long id) {
        try {
            typeJPA.delete(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Type findByname(String name) {
        Type byName = typeJPA.findByName(name);
        return byName;
    }



}
